#
#    Copyright (C) 2016 Francisco Javier Parra <franparpe@openmailbox.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

from PyQt5.QtWidgets import QFrame
from PyQt5.QtWidgets import QGroupBox
from PyQt5.QtWidgets import QGridLayout
from PyQt5.QtWidgets import QLabel, QSpinBox, QLineEdit

from PyQt5 import QtCore

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s


class CamData(QFrame):
    def __init__(self, parent):
        "Initializing"
        super(CamData, self).__init__(parent)

        # Aesthetics
        self.setFrameShape(self.StyledPanel)
        self.setFrameShadow(self.Raised)

        # Group Box
        self.groupBox = QGroupBox(self)
        self.groupBox.setTitle(_fromUtf8("Cam Data"))

        # The layout
        self.mainGrid = QGridLayout(self)
        self.mainGrid.addWidget(self.groupBox, 0, 0, 1, 1)

        # initializing for the new grid
        self.camDataGrid = QGridLayout(self.groupBox)

        # Labels
        self.angularVelLabel = QLabel(self.groupBox)
        self.angularVelLabel.setText(_fromUtf8("Angular velocity [rad/s]"))
        self.numberOfSectionsLab = QLabel(self.groupBox)
        self.numberOfSectionsLab.setText(_fromUtf8("Number of sections"))

        # Line Edit
        self.angularVelLEdit = QLineEdit(self.groupBox)

        # Spin box
        self.numberOfSectSpin = QSpinBox(self.groupBox)

        # Placing everything in the layout
        self.camDataGrid.addWidget(self.angularVelLabel, 1, 0, 1, 1)
        self.camDataGrid.addWidget(self.angularVelLEdit, 1, 1, 1, 1)
        self.camDataGrid.addWidget(self.numberOfSectionsLab, 0, 2, 1, 1)
        self.camDataGrid.addWidget(self.numberOfSectSpin, 1, 2, 1, 1)

    def getAngularVelocity(self):
        """ Returns the angular velocity from the box
        """
        return float(self.angularVelLEdit.text())

    def getNumberOfSections(self):
        """ Returns the number of sections the cam has
        """
        return int(self.numberOfSectionsSpin.value())
    
    def connectNsections(self, function):
        """connect the action with the combobox
        """
        self.numberOfSectSpin.valueChanged.connect(function)
